package com.chaoyong.higo.adapter;


import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chaoyong.higo.R;
import com.chaoyong.higo.bean.ShopsBean.DataEntity.ListEntity;
import com.chaoyong.higo.utils.TransferTool;
import com.chaoyong.higo.utils.Values;
import com.lidroid.xutils.BitmapUtils;

public class ShopsAdapter extends BaseAdapter {
	private Context context;
	private List<ListEntity> bean;
	private LayoutInflater layoutInflater; 
	private HolderClickListener mHolderClickListener;  
	private HolderListener holderListener;
	
	
	public ShopsAdapter(Context context,List<ListEntity> bean) {
		super();
		this.context = context;
		this.bean=bean;
		layoutInflater = layoutInflater.from(context);
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return bean.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder Holder;
		String price=bean.get(position).getGoods_price();
//		String name=bean.getData().getList().get(position).getGoods_name();
		int count=bean.get(position).getSurplus();
		
		if (convertView!=null && convertView.getTag()!=null) {
			Holder = (ViewHolder) convertView.getTag();
		}
		else {
			convertView=layoutInflater.inflate(R.layout.item_shops,null);
			Holder=new ViewHolder();
			Holder.shop_imgbt=
					(ImageView) convertView.findViewById(R.id.itemshop_shop_imgbt);
			Holder.imgview=
					(ImageView) convertView.findViewById(R.id.itemshop_image_img);
			Holder.itemshop_lay=(LinearLayout) convertView.findViewById(R.id.itemshop_lay);
			Holder.price_tx=(TextView) convertView.findViewById(R.id.itemshops_price_tx);
			Holder.name_tx=(TextView) convertView.findViewById(R.id.itemshops_name_tx);
			Holder.total_tx=(TextView) convertView.findViewById(R.id.itemshops_total_tx);
			Holder.left_tx=(TextView) convertView.findViewById(R.id.itemshops_left_tx);
			Holder.count_tx=(TextView) convertView.findViewById(R.id.itemshops_count_tx);
			Holder.test_progressbar=(ProgressBar) convertView.findViewById(R.id.test_progressbar);
			convertView.setTag(Holder);
		}
		Holder.shop_imgbt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mHolderClickListener!=null){  
                    int[] start_location = new int[2];  
                    Holder.imgview.getLocationInWindow(start_location);//获取点击商品图片的位置  
                    Drawable drawable = Holder.imgview.getDrawable();//复制一个新的商品图标  
                    mHolderClickListener.onHolderClick(drawable,start_location);  
                }  
			}
		});
		Holder.itemshop_lay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (holderListener!=null) {
					holderListener.onSetHolderClick();
				}
			}
		});
		Holder.price_tx.setText(bean.get(position).getGoods_price());
		Holder.name_tx.setText(bean.get(position).getGoods_name());
		Holder.total_tx.setText(TransferTool.getString(bean.get(position).getGoods_price(), "."));
		Holder.count_tx.setText(TransferTool.toString(count));
		Holder.left_tx.setText(TransferTool.toString(TransferTool.Strtoint(price,".")-count));
		Double d=(double) (count/(TransferTool.Strtoint(price, ".")/100));
		Holder.test_progressbar.setProgress(d.intValue());
		BitmapUtils tUtil = new BitmapUtils(context);
		tUtil.display(Holder.imgview,Values.BASE_IMAGE_URL+bean.get(position).getFace_img().get(0));
		return convertView;
	}
	class ViewHolder
	{
		ImageView shop_imgbt;
		ImageView imgview;
		LinearLayout itemshop_lay;
		TextView price_tx;
		TextView name_tx;
		TextView total_tx;
		TextView left_tx;
		TextView count_tx;
		ProgressBar test_progressbar;
	}
	public void SetOnSetHolderClickListener(HolderClickListener holderClickListener){  
        this.mHolderClickListener = holderClickListener;  
    }  
    public interface HolderClickListener{  
        public void onHolderClick(Drawable drawable,int[] start_location);  
    }
    public void SetOnHolderClickListener(HolderListener holderListener){  
    	this.holderListener = holderListener;  
    }  
    public interface HolderListener{  
    	public void onSetHolderClick();  
    }

}
